package com.carAnalysisAdmin.system.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.carAnalysisAdmin.system.domain.SysParkSpot;
import com.carAnalysisAdmin.system.service.ISysParkSpotService;
import com.carAnalysisAdmin.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.carAnalysisAdmin.common.annotation.Log;
import com.carAnalysisAdmin.common.core.controller.BaseController;
import com.carAnalysisAdmin.common.core.domain.AjaxResult;
import com.carAnalysisAdmin.common.enums.BusinessType;
import com.carAnalysisAdmin.system.domain.UserSpotInfo;
import com.carAnalysisAdmin.system.service.IUserSpotInfoService;
import com.carAnalysisAdmin.common.utils.poi.ExcelUtil;
import com.carAnalysisAdmin.common.core.page.TableDataInfo;

/**
 * 用户和车位关联信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-20
 */
@RestController
@RequestMapping("/system/userSpotInfo")
public class UserSpotInfoController extends BaseController
{
    @Autowired
    private IUserSpotInfoService userSpotInfoService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysParkSpotService sysParkSpotService;

    /**
     * 查询用户和车位关联信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserSpotInfo userSpotInfo)
    {
        startPage();
        List<UserSpotInfo> list = userSpotInfoService.selectUserSpotInfoList(userSpotInfo);
        return getDataTable(list);
    }

    @GetMapping("/getUserSpotList")
    public TableDataInfo getUserSpotList(UserSpotInfo userSpotInfo)
    {
        startPage();
        userSpotInfo.setUserId(getUserId());
        List<UserSpotInfo> list = userSpotInfoService.selectUserSpotInfoList(userSpotInfo);
        return getDataTable(list);
    }

    @GetMapping("/getUserSpotListWithUser")
    public AjaxResult getUserSpotListWithUser()
    {
        JSONObject result = new JSONObject();
        UserSpotInfo userSpotInfo = userSpotInfoService.selectUserSpotInfoByUserId(getUserId());
        if (userSpotInfo == null){
            return error("用户没有绑定车位");
        }
        SysParkSpot sysParkSpot = sysParkSpotService.selectSysParkSpotByCode(userSpotInfo.getSpotCode());
        Integer floorId = sysParkSpot.getFloorId();
        result.put("floorId", floorId);
        result.put("spotCode", userSpotInfo.getSpotCode());
        return success(result);
    }

    /**
     * 导出用户和车位关联信息列表
     */
    @Log(title = "用户和车位关联信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserSpotInfo userSpotInfo)
    {
        List<UserSpotInfo> list = userSpotInfoService.selectUserSpotInfoList(userSpotInfo);
        ExcelUtil<UserSpotInfo> util = new ExcelUtil<UserSpotInfo>(UserSpotInfo.class);
        util.exportExcel(response, list, "用户和车位关联信息数据");
    }

    /**
     * 获取用户和车位关联信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userSpotInfoService.selectUserSpotInfoById(id));
    }

    /**
     * 新增用户和车位关联信息
     */
    @Log(title = "用户和车位关联信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserSpotInfo userSpotInfo)
    {
        return toAjax(userSpotInfoService.insertUserSpotInfo(userSpotInfo));
    }

    @PostMapping("/putIntoStorage")
    public AjaxResult putIntoStorage(@RequestBody UserSpotInfo userSpotInfo)
    {
        userSpotInfo.setUserId(getUserId());
        userSpotInfo.setUserName(getUsername());
        userSpotInfo.setUserAvatar(userService.selectUserById(getUserId()).getAvatar());
        return toAjax(userSpotInfoService.insertUserSpotInfo(userSpotInfo));
    }

    /**
     * 修改用户和车位关联信息
     */
    @Log(title = "用户和车位关联信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserSpotInfo userSpotInfo)
    {
        return toAjax(userSpotInfoService.updateUserSpotInfo(userSpotInfo));
    }

    /**
     * 删除用户和车位关联信息
     */
    @Log(title = "用户和车位关联信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userSpotInfoService.deleteUserSpotInfoByIds(ids));
    }
    @PostMapping("/getMoney")
    public AjaxResult getMoney()
    {
        UserSpotInfo userSpotInfo = userSpotInfoService.selectUserSpotInfoByUserId(getUserId());
        if (userSpotInfo != null){
            SysParkSpot sysParkSpot = sysParkSpotService.selectSysParkSpotByCode(userSpotInfo.getSpotCode());
            BigDecimal price = sysParkSpot.getPrice();
            Date date = new Date();
            Date warehousingTime = userSpotInfo.getWarehousingTime();
            long hourMoney = (date.getTime() - warehousingTime.getTime()) * price.longValue();

        }

        return toAjax(1);
    }


}
